/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/8/17 21:05:20
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 处理包含标签
    /// </summary>
    public class IncludeLabel : AbstractLabelProvider
    {
        public IncludeLabel(string label)
            : base(label)
        {

        }

        /// <summary>
        /// 获取标签表示的Html代码段
        /// </summary>
        /// <returns></returns>
        public override string GetHtml()
        {
            try
            {
                string pathPara = LabelProviderHelper.GetProperties(LabelString)[0];

                if (!pathPara.StartsWith("~"))
                {
                    if (Context.TemplateVirtualDirectory != null)
                    {
                        pathPara = pathPara.TrimStart('/');
                        pathPara = System.Web.VirtualPathUtility.Combine(Context.TemplateVirtualDirectory, pathPara);
                    }
                }

                string filepath = System.Web.Hosting.HostingEnvironment.MapPath(pathPara);

                if (File.Exists(filepath))
                {
                    return File.ReadAllText(filepath);
                }
                return LabelString;
            }
            catch (Exception)
            {
                return LabelString;
            }
        }
    }
}
